package com.soilflow.framework.web.service;

import com.alibaba.fastjson2.JSON;
import com.soilflow.common.core.domain.entity.SysUserReception;
import com.soilflow.common.core.redis.RedisCache;
import com.soilflow.common.domain.ColorfulUser;
import com.soilflow.common.domain.DahuBusiness;
import com.soilflow.system.service.IColorfulUserService;
import com.soilflow.system.service.IDahuBusinessService;
import com.soilflow.system.service.ISysUserReceptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.common.core.domain.model.LoginUser;
import com.soilflow.common.enums.UserStatus;
import com.soilflow.common.exception.ServiceException;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.system.service.ISysUserService;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户验证处理
 *
 * @author soilflow
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IColorfulUserService colorfulUserService;

    @Autowired
    private ISysUserReceptionService userReceptionService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private IDahuBusinessService businessService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        System.out.println("username ======= " + username);
        if (username.contains("_Duocai")){
            username= username.replace("_Duocai", "");
            ColorfulUser user = colorfulUserService.selectUserByUserName(username);
            SysUser sysUser = JSON.parseObject(JSON.toJSONString(user), SysUser.class);
            System.out.println("user = " + user);
            if (StringUtils.isNull(user)) {
                log.info("登录用户：{} 不存在.", username);
                throw new ServiceException("登录用户：" + username + " 不存在");
            } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
                log.info("登录用户：{} 已被删除.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("登录用户：{} 已被停用.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已停用");
            }
            return createLoginUser(sysUser);
        }
        if (username.contains("_dahu")){
            username= username.replace("_dahu", "");
            DahuBusiness user = businessService.selectUserByUserName(username);
            SysUser sysUser = JSON.parseObject(JSON.toJSONString(user), SysUser.class);
            System.out.println("user = " + user);
            if (StringUtils.isNull(user)) {
                log.info("登录用户：{} 不存在.", username);
                throw new ServiceException("登录用户：" + username + " 不存在");
            } else if (UserStatus.DELETED.getCode().equals(user.getDeleted())) {
                log.info("登录用户：{} 已被删除.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("登录用户：{} 已被停用.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已停用");
            }
            return createLoginUser(sysUser);
        }
        if (!username.contains("_Reception")) {
            SysUser user = userService.selectUserByUserName(username);
            if (StringUtils.isNull(user)) {
                log.info("登录用户：{} 不存在.", username);
                throw new ServiceException("登录用户：" + username + " 不存在");
            } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
                log.info("登录用户：{} 已被删除.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("登录用户：{} 已被停用.", username);
                throw new ServiceException("对不起，您的账号：" + username + " 已停用");
            }
            return createLoginUser(user);
        }else {
            username= username.replace("_Reception", "");
            SysUserReception sysUserReception = userReceptionService.selectUserByUserName(username);
            SysUser sysUser = JSON.parseObject(JSON.toJSONString(sysUserReception), SysUser.class);
                if (StringUtils.isNull(sysUser)) {
                    log.info("登录用户：{} 不存在.", username);
                    throw new ServiceException("登录用户：" + username + " 不存在");
                } else if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) {
                    log.info("登录用户：{} 已被删除.", username);
                    throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
                } else if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
                    log.info("登录用户：{} 已被停用.", username);
                    throw new ServiceException("对不起，您的账号：" + username + " 已停用");
                }
            return createLoginUser(sysUser);
        }
    }

    public UserDetails createLoginUser(SysUser user)
    {
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
    }

    public UserDetails createLoginUser(SysUserReception user)
    {
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
    }
    public UserDetails createColorfuLoginUser(ColorfulUser colorfulUser)
    {
        return new LoginUser(colorfulUser.getUserId(),  colorfulUser);
    }
}
